package com.overseasShopping.Base.DAOImpl;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.overseasShopping.Base.DAO.UserDAO;
import com.overseasShopping.pojo.User;
import com.overseasShopping.util.ListUtil;
import com.overseasShopping.util.SessionFactoryUtil;

public class UserDAOImpl extends BaseDAOImpl<User> implements UserDAO{
	//登录
	public User login(String id, String password) {
		String hql="from User where userId=? and password=?";
		Object[] params={id,password};
		List<User> userList=getHibernateTemplate().find(hql, params);
		return userList!=null&&userList.size()>0?userList.get(0):null;
	}
	
	//根据id查询用户
	public User getUser(String id) {
		return (User) getHibernateTemplate().get(User.class, id);
	}
	//黑名单
	public List<User> getBlackFriends(String id) {
		User user=(User) getHibernateTemplate().get(User.class, id);
		List<User>  users=ListUtil.getInstance().setToList(user.getBlackList()) ;
		Collections.sort(users, new Comparator<User>() {

			public int compare(User o1, User o2) {
				if(o1.getUserId().compareTo(o2.getUserId())<0){
					return 1;
				}
				return -1;
			}
			
		});
		return users;
	}
	
	public List<User> getFriends(String id) {
		User user=(User) getHibernateTemplate().get(User.class, id);
		List<User> users=ListUtil.getInstance().setToList(user.getFriends());
		Collections.sort(users, new Comparator<User>() {
			public int compare(User o1, User o2) {
				if (o1.getUserId().compareTo(o2.getUserId())<0) {
					return 1;
				}
				return -1;
			}
		});
		for(User user1 : users){
			user1.getUserId();
			user1.getDetailUser();
			user1.getBlackList();
			user1.getComplaint();
			user1.getFriends();
			user1.getOrderLists();
			user1.getWallet();
		}
		return  users;
	}
	//添加好友
	public void addFriend(String uid, String fid) {
		User friend=new User(fid);
		User user=get(uid);
		Set<User> friends=user.getFriends();
		friends.add(friend);
		user.setFriends(friends);
		getHibernateTemplate().save(user);
	}
	
	//删除好友
	public void deleteFriend(String uid,String fid){
		User user=get(uid);
		User friend=get(fid);
		Set<User> friends = user.getFriends();
		friends.remove(friend);
		user.setFriends(friends);
		getHibernateTemplate().save(user); 
	}
	//添加黑名单
	public void addBlackList(String uid,String bid){
		User blackUser=get(bid);
		
		User user=get(uid);
		
		Set<User> blackUsers=user.getBlackList();
		blackUsers.add(blackUser);
		user.setBlackList(blackUsers);
		getHibernateTemplate().save(user);
	}
	
	//移除黑名单
		public void deleteBlackUser(String uid, String bid) {
			User blackUser=get(bid);
			User user=get(uid);
			Set<User> blackUsers=user.getBlackList();
			blackUsers.remove(blackUser);
			user.setBlackList(blackUsers);
			getHibernateTemplate().save(user);
		}

		public void updateUser(User user) {
			getHibernateTemplate().update(user);
		}

}
